package cn.lanqiao.book.controller;

import cn.lanqiao.book.dao.IAdminDao;
import cn.lanqiao.book.dao.IBookDao;
import cn.lanqiao.book.dao.ICategoryDao;
import cn.lanqiao.book.dao.IChapterDao;
import cn.lanqiao.book.dao.impl.AdminDaoImplI;
import cn.lanqiao.book.dao.impl.BookDaoImplI;
import cn.lanqiao.book.dao.impl.CategoryDaoImplI;
import cn.lanqiao.book.dao.impl.ChapterDaoImplI;
import cn.lanqiao.book.pojo.Admin;
import cn.lanqiao.book.pojo.Book;
import cn.lanqiao.book.pojo.category;
import cn.lanqiao.book.pojo.chapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class findAll extends HttpServlet {
    Admin ad = new Admin();
    IAdminDao add = new AdminDaoImplI();
    Book bo = new Book();
    IBookDao bd = new BookDaoImplI();
    ICategoryDao cd = new CategoryDaoImplI();
    category c = new category();
    IChapterDao chd = new ChapterDaoImplI();
    chapter ch = new chapter();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("_method");
        try {
            Method m = getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void finishbook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();

        List<Book> lb = new ArrayList<>();
        lb = bd.finishbook();
        Map<Book, String> categorymap = new HashMap<>();
        Map<Book, String> chaptermap = new HashMap<>();
        Map<Book, Integer> chidrmap = new HashMap<>();

        int cid;
        int bid;
        String cname;
        String chname;
        int chid;
        for (Book book : lb) {
            cid = book.getC_id();
            bid = book.getB_id();
            cname = cd.queryForOnebyid(cid).getC_name();

            categorymap.put(book, cname);
            chname = chd.queryForOnebyid(bid).getCh_name();
            chid = chd.queryForOnebyid(bid).getCh_id();
            chaptermap.put(book, chname);
            chidrmap.put(book, chid);

        }
        req.setAttribute("book", lb);
        req.setAttribute("categorymap", categorymap);
        req.setAttribute("chaptermap", chaptermap);
        req.setAttribute("chid", chidrmap);
        req.getRequestDispatcher("/finishnovel.jsp").forward(req, resp);

    }

    public void notfinishbook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();

        List<Book> lb = new ArrayList<>();
        lb = bd.notfinishbook();
        Map<Book, String> categorymap = new HashMap<>();
        Map<Book, String> chaptermap = new HashMap<>();
        int cid;
        int bid;
        String cname;
        String chname;
        for (Book book : lb) {
            cid = book.getC_id();
            bid = book.getB_id();
            cname = cd.queryForOnebyid(cid).getC_name();
            categorymap.put(book, cname);
            chname = chd.queryForOnebyid(bid).getCh_name();
            chaptermap.put(book, chname);
        }
        req.setAttribute("book", lb);
        req.setAttribute("categorymap", categorymap);
        req.setAttribute("chaptermap", chaptermap);
        req.getRequestDispatcher("/finish.jsp").forward(req, resp);
    }

    public void bookdatil(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        String name = req.getParameter("b_name1");
        bo = bd.queryForOne(name);
        int id = bo.getB_id();
        List<chapter> lch = new ArrayList<>();
        lch = chd.content(id);
        int i = 1;
        Map<chapter, Integer> lnum = new HashMap();
        for (chapter c : lch) {
            lnum.put(c, i);
            i++;
        }


        String chname;
        int bid = bo.getB_id();
        chname = chd.queryForOnebyid(bid).getCh_name();
        c = cd.queryForOnebyid(bo.getC_id());
        String cname = c.getC_name();

        String sta;
        if (bo.getB_state() == 1) {
            sta = "连载中";
        } else {
            sta = "已完结";
        }


        req.setAttribute("num", lnum);
        req.setAttribute("sta", sta);
        req.setAttribute("cname", cname);
        req.setAttribute("newcontent", chname);
        req.setAttribute("chapter", lch);
        req.setAttribute("book", bo);
        req.getRequestDispatcher("/chapter.jsp").forward(req, resp);
    }

    public void findBookBycid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        int cid = Integer.parseInt(req.getParameter("c_id"));

        switch (cid) {
            case 1:
                req.getRequestDispatcher("/fantasy.jsp").forward(req, resp);
                break;
            case 2:
                req.getRequestDispatcher("/wxxx.jsp").forward(req, resp);
                break;
            case 3:
                req.getRequestDispatcher("/romance.jsp").forward(req, resp);
                break;
            case 4:
                req.getRequestDispatcher("/historymilitary.jsp").forward(req, resp);
                break;
            case 5:
                req.getRequestDispatcher("/science.jsp").forward(req, resp);
                break;
            case 6:
                req.getRequestDispatcher("/olgame.jsp").forward(req, resp);
                break;
            case 7:
                req.getRequestDispatcher("/femate.jsp").forward(req, resp);
                break;
            case 8:
                req.getRequestDispatcher("/fantasy.jsp").forward(req, resp);
                break;
            case 9:
                req.getRequestDispatcher("/fantasy.jsp").forward(req, resp);
                break;
            case 10:
                req.getRequestDispatcher("/fantasy.jsp").forward(req, resp);
                break;
        }
    }

    public void searchbyab(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("u_username");

        if (name == null) {
            name = "";
        }

        name = "%" + name + "%";
        List<Book> l = new ArrayList<>();
        l = bd.searchbookbyab(name);
        if (l != null) {
            Map<Book, String> categorymap = new HashMap<>();
            Map<Book, String> chaptermap = new HashMap<>();
            Map<Book, Integer> chidrmap = new HashMap<>();

            int cid;
            int bid;
            String cname;
            String chname;
            int chid;
            for (Book book : l) {
                cid = book.getC_id();
                bid = book.getB_id();
                cname = cd.queryForOnebyid(cid).getC_name();
                categorymap.put(book, cname);
                chname = chd.queryForOnebyid(bid).getCh_name();
                chid = chd.queryForOnebyid(bid).getCh_id();
                chaptermap.put(book, chname);
                chidrmap.put(book, chid);
                req.setAttribute("book", l);
                req.setAttribute("categorymap", categorymap);
                req.setAttribute("chaptermap", chaptermap);
                req.setAttribute("chid", chidrmap);

            }
            req.getRequestDispatcher("/search.jsp").forward(req, resp);
        }

    }

}
